import constants from '@/constants';
import { BaseService } from '@/core/base.service';
import { ChainTxStateEnum } from '@/enums';
import { halt } from '@/utils';
import { InjectQueue, Process, Processor } from '@nestjs/bull';
import { Job, Queue } from 'bull';

@Processor('chain')
export class ChainProcessor extends BaseService {
  constructor(
    @InjectQueue('chain')
    private readonly chainQueue: Queue,
  ) {
    super();
  }

  @Process('deploy')
  async deploy(job: Job<{ operationId: string }>) {
    const { operationId } = job.data;
    const txRes = await this.chain.nftTx({
      operationId,
    });
    if (txRes?.code !== 200) {
      console.log(`查询资产类别结果失败: ${txRes?.message ?? '未知错误'}`);
    } else {
      const txHash = txRes.data.txHash;
      const txState = txRes.data.state;
      console.log(`资产类别: ${operationId} - ${txState}`);
      await this.prisma.asset.updateMany({
        where: {
          operationId,
        },
        data: {
          txHash,
          txState,
        },
      });
      if (txState === ChainTxStateEnum.PENDING) {
        console.log(`铸造重试: ${operationId}`);
        await this.chainQueue.add(
          'deploy',
          {
            operationId,
          },
          {
            delay: 3000,
          },
        );
      }
    }
  }

  @Process('mint')
  async mint(job: Job<{ id: number }>) {
    const { id } = job.data;
    const token = await this.prisma.token.findUnique({
      where: {
        id,
      },
    });
    if (!token || token.txState === ChainTxStateEnum.SUCCESS) {
      return;
    }
    const txRes = await this.chain.nftTx({
      operationId: token.operationId,
    });
    if (txRes?.code !== 200) {
      console.log(`查询铸造结果失败: ${txRes?.message ?? '未知错误'}`);
    } else {
      const txHash = txRes.data.txHash;
      const txState = txRes.data.state;
      console.log(`铸造查询: ${token.operationId} - ${txState}`, txRes);
      await this.prisma.token.updateMany({
        where: {
          id,
        },
        data: {
          txHash,
          txState,
        },
      });
    }
  }

  @Process('transfer')
  async transfer(job: Job<{ operationId: string }>) {
    // const { operationId } = job.data;
    // const txRes = await this.chain.nftTx({
    //   operationId,
    // });
    // if (txRes?.code !== 200) {
    //   console.log(`查询转让结果失败: ${txRes?.message ?? '未知错误'}`);
    // } else {
    //   const txHash = txRes.data.txHash;
    //   const txState = txRes.data.state;
    //   console.log(`转让: ${operationId} - ${txState}`);
    //   await this.prisma.token.update({
    //     where: {
    //       txOperationId: operationId,
    //     },
    //     data: {
    //       txHash,
    //       txState,
    //     },
    //   });
    //   if (txState === ChainTxStateEnum.PENDING) {
    //     console.log(`转让重试: ${operationId}`);
    //     await this.chainQueue.add(
    //       'transfer',
    //       {
    //         operationId,
    //       },
    //       {
    //         delay: 3000,
    //       },
    //     );
    //   }
    // }
    // const operationId = await this.chain.generateOperationId();
    // const res = await this.chain.nftTransfer(
    //   {
    //     classId: o.token.classId,
    //     tokenId: o.token.tokenId,
    //     from: o.seller.account,
    //     recipient: user.account,
    //     operationId,
    //   },
    //   {
    //     'app-user-code': o.seller.mobile,
    //   },
    // );
    // if (res?.code !== 200) {
    //   return halt(`资产转移失败: ${res?.message ?? '未知错误'}`);
    // }
    // await sleep(50);
    // await this.chainQueue.add(
    //   'transfer',
    //   {
    //     operationId,
    //   },
    //   {
    //     delay: 3000,
    //   },
    // );
  }
}
